import axios from 'axios';

let configBaseUrl = window.Config.baseUrl;
let configAppToken = window.Config.appToken;
let configImgBucket = window.Config.imgBucket;
let configImgKey = window.Config.imgKey;
// 定义地址
//登录
let HTTP = axios.create({
//	baseURL:'',
	baseURL:configBaseUrl,
	headers: {
		'appToken':configAppToken
	}
})
//获取cookie里面的token
function getCookie(name)
{
	var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
	if(arr=document.cookie.match(reg)){
		return unescape(arr[2]);
	}else{	
		return null;
	}
}
function delCookie (name){   
	var exp = new Date();   
	exp.setTime(exp.getTime() - 1);   
	var cval= this.getCookie(name);   
	if(cval!=null)   
	document.cookie= name + "="+cval+"; path=/;expires="+exp.toGMTString();   
}
//API网管异常处理
function errorHandle(msg,params){
	if(msg){
		if(msg.code != '1101'){
			alert('系统异常！');
		}else{
			delCookie('token');
			params.$router.push('/');
		}
	}
}
export const checkLogin = params => {
	var userToken = getCookie("token");
	if(userToken == null){
		params.$router.push({ path: '/'});
		return false;
	}
	HTTP.defaults.headers.common ['token'] = userToken;
	
	return true;
}
	
//接口管理列表
export const interfaceAdmin = () => {
	return HTTP.get('/appManageService/interface/list').then(response => response.data).catch(function (error) {
     errorHandle(error.response.data,this);
 });
}	
//接口添加
export const interfaceAdd = params => {
	console.log('接口添加向后台传参........',params);
//	return HTTP.post('/AppinInterfaceManageService/interface/add?name='+params.name
//	+'&desc='+params.desc+'&url='+params.address+'&version='+params.version
//	).then(response => response.data).catch(function (error) {
//      errorHandle(error.response.data,this);
//  });
	return HTTP.get('/static/mockJson/handle.json').then(response => response.data);
}	
//接口删除
export const interfaceDel = params => {
	console.log('接口删除向后台传参........',params);
//	return HTTP.post('/AppinInterfaceManageService/interface/delete/'+params.id).then(response => response.data).catch(function (error) {
//      errorHandle(error.response.data,this);
//  });
	return HTTP.get('/static/mockJson/handle.json').then(response => response.data);
}
//接口修改
export const interfaceEdit = params => {
	console.log('接口修改向后台传参........',params);
//	return HTTP.post('/AppinInterfaceManageService/interface/update/'+params.id+'?name='+params.name+'&desc='+params.desc+'&url='+params.url+'&version='+params.version).then(response => response.data).catch(function (error) {
//      errorHandle(error.response.data,this);
//  });
	return HTTP.get('/static/mockJson/handle.json').then(response => response.data);
}
//接口状态修改
export const interfaceStatus = params => {
	console.log('接口状态改动向后台传参........',params);
//	return HTTP.post('/AppinInterfaceManageService/interface/updateStatus/'+params.id+'?status='+params.status).then(response => response.data).catch(function (error) {
//      errorHandle(error.response.data,this);
//  });
	return HTTP.get('/static/mockJson/handle.json').then(response => response.data);
}
//未授权接口列表
export const interfaceList = params => {
	console.log('未授权接口列表向后台传参........',params);
//	return HTTP.get('/AppinInterfaceManageService/interfaceAuth/getUnAuthInterfaceListByAppId/'+params.appId,{params:{
//		pageNumber : params.pageNumber,
//		pageSize : params.pagesize
//	}}).then(response => response.data).catch(function (error) {
//      errorHandle(error.response.data,this);
//  });
	return HTTP.get('/static/mockJson/list.json').then(response => response.data);
}


//应用管理图片上传
export const uploadPic = params => {
	let config = {  
	    headers:{'Content-Type':'multipart/form-data'}  
	}; 
	//HTTP.defaults.headers.common ['Content-Type'] = "multipart/form-data";
	console.log('应用管理列表向后台上传图片参数........',params);
	return HTTP.post('/appinimageservice/storage/222/333/444/555',params).then(response => response.data).catch(function (error) {
        errorHandle(error.response.data,this);
    });
//	return HTTP.get('/static/mockJson/apply.json').then(response => response.data);
}
//应用管理列表
export const applyList = params => {
	return HTTP.get('/appManageService/app/list').then(response => response.data).catch(function (error) {
     errorHandle(error.response.data,this);
 });
	// return HTTP.get('/static/mockJson/apply.json').then(response => response.data);
}
//应用授权管理(已授权)
export const addAdmin = params => {
	console.log('应用授权管理向后台传参........',params);
//	return HTTP.get('/AppinInterfaceManageService/interfaceAuth/getAuthInterfaceListByAppId/'+params.appId,{
//		params:{
//			pageNumber:params.pageNumber,
//			pageSize:params.pageSize			
//		}
//	}).then(response => response.data).catch(function (error) {
//      errorHandle(error.response.data,this);
//  });
	return HTTP.get('/static/mockJson/interface.json').then(response => response.data);
}	
//对应用进行授权(未授权的应用授权)
export const addImpower = params => {
	console.log('应用进行授权向后台传参...',params);
//			return HTTP.post('/AppinAppManageService/app/authorize/'+params.id).then(response => response.data).catch(function (error) {
//      errorHandle(error.response.data,this);
//  });
	return HTTP.get('/static/mockJson/handle.json').then(response => response.data);
}
//取消应用的授权
export const delApplyImpower = params => {
	console.log('取消应用授权向后台传参。。。',params);
//			return HTTP.post('/AppinAppManageService/app/removeAuthorize/'+params.appId).then(response => response.data).catch(function (error) {
//      errorHandle(error.response.data,this);
//  });
	return HTTP.get('/static/mockJson/handle.json').then(response => response.data);
}
//应用的创建
export const creatApplication =params => {
	console.log('应用添加向后台传参........',params);
//	 return HTTP.post ('/AppinAppManageService/app/add?name='+params.name+'&description='+params.description+'&logoUrl='+params.logoUrl).then(response => response.data).catch(function (error) {
//      errorHandle(error.response.data,this);
//  });
	return HTTP.get('/static/mockJson/handle.json').then(response => response.data);
}
//应用的删除
export const deleteApplication =params => {
	console.log('应用的删除向后台传参........',params);
//	 return HTTP.post ('/AppinAppManageService/app/delete/'+params.id).then(response => response.data).catch(function (error) {
//      errorHandle(error.response.data,this);
//  });
	return HTTP.get('/static/mockJson/handle.json').then(response => response.data);
}
//应用的修改
export const editApplication =params => {
	console.log('应用的修改向后台传参........',params);
//	 return HTTP.post ('/AppinAppManageService/app/update/'+params.id+'?name='+params.name+'&description='+params.description+'&logoUrl='+params.logoUrl).then(response => response.data).catch(function (error) {
//      errorHandle(error.response.data,this);
//  });
	return HTTP.get('/static/mockJson/handle.json').then(response => response.data);
}
//取消授权
export const deleteAuth =params => {
	console.log('应用的取消接口授权向后台传参........',params);
//	 return HTTP.post ('/AppinInterfaceManageService/interfaceAuth/delete/'+params.appId+'/'+params.interfaceId).then(response => response.data).catch(function (error) {
//      errorHandle(error.response.data,this);
//  });
	return HTTP.get('/static/mockJson/handle.json').then(response => response.data);
}
//批量增加授权
export const addAuth = params => {
	console.log('批量增加授权向后台传参........',params);
//	return HTTP.post('/AppinInterfaceManageService/interfaceAuth/batchAdd?appId='+params.appId+'&interfaceIds='+params.interfaceIds).then(response => response.data).catch(function (error) {
//      errorHandle(error.response.data,this);
//  });
	return HTTP.get('/static/mockJson/handle.json').then(response => response.data);
}



//应用监控饼状图
export const appCookie = params => {
	return HTTP.get('/appManageService/appCookie/list').then(response => response.data).catch(function (error) {
    	errorHandle(error.response.data,this);
 	});
}	
//应用监控柱状图
export const appPillar = () => {
		return HTTP.get('/appManageService/appPillar/list').then(response => response.data).catch(function (error) {
		errorHandle(error.response.data,this);
	});
}
//应用监控列表
export const appList = () => {
	return HTTP.get('/appManageService/applist/list').then(response => response.data).catch(function (error) {
     errorHandle(error.response.data,this);
 });
}	
//应用监控详情表
export const applyDetail = params => {
	console.log('应用监控详情向后台传参........',params);	
//	return HTTP.get('/AppinDataMonitorService/app/accessReportDetail',{params:params}).then(response => response.data).catch(function (error) {
//      errorHandle(error.response.data,this);
//  });
	return HTTP.get('/static/mockJson/appdetail.json').then(response => response.data);
}	
//应用监控日志
export const apprizhi = params => {
	console.log('应用监控日志向后台传参........',params);
//	return HTTP.get('/AppinDataMonitorService/app/accessLog',{params:params}).then(response => response.data).catch(function (error) {
//      errorHandle(error.response.data,this);
//  });
	return HTTP.get('/static/mockJson/apprizhi.json').then(response => response.data);
}	


//接口监控列表
export const interfaceUse = () => {
	return HTTP.get('/appManageService/inter/list').then(response => response.data).catch(function (error) {
     errorHandle(error.response.data,this);
 });
}	
//接口监控详情列表
export const interfaceDetail = params => {
	console.log("详情数据向后台传参........",params);
//	return HTTP.get('/AppinDataMonitorService/interface/accessReportDetail',{params:params}).then(response => response.data).catch(function (error) {
//      errorHandle(error.response.data,this);
//  });
	return HTTP.get('/static/mockJson/interfaceDetail.json').then(response => response.data);
}	
//接口监控日志查看
export const interfacerizhi = params => {
	console.log("日志数据向后台传参........",params);
//	return HTTP.get('/AppinDataMonitorService/interface/accessLog',{params:params}).then(response => response.data).catch(function (error) {
//      errorHandle(error.response.data,this);
//  });
	return HTTP.get('/static/mockJson/interfacerizhi.json').then(response => response.data);
}
//接口监控饼状图
export const interfaceCookie = () => {
	return HTTP.get('/appManageService/interfaceCookie/list',).then(response => response.data).catch(function (error) {
     errorHandle(error.response.data,this);
 });
}
//接口监控柱状图
export const interfacePillar = () => {
		return HTTP.get('/appManageService/interfacePillar/list').then(response => response.data).catch(function (error) {
     errorHandle(error.response.data,this);
 });
}



//验证登陆
export const requestLogin = () => {
	return HTTP.get('/appManageService/login').then(response => response.data).catch(function (error) {
		errorHandle(error.response.data,this);
	});;
}

export const userInfo = () => {
	return HTTP.get('/appManageService/userInfo/info',).then(response => response.data).catch(function (error) {
     errorHandle(error.response.data,this);
 });
}

//获取图片上传地址
export const getUploadUrl = params => {
	return configBaseUrl + "/AppinImageService/storage/upload";
//	return HTTP.get('/static/mockJson/config.json').then(response => response.data);
}

//获取图片预览地址
export const getViewUrl = params => {
	return configBaseUrl + "/AppinImageService/storage/"+configImgBucket+"/"+configImgKey+"/"+params+"?json=";
//	return HTTP.get('/static/mockJson/config.json').then(response => response.data);
}

//获取图片bucket
export const getImgBucket = params => {
	return configImgBucket;
//	return HTTP.get('/static/mockJson/config.json').then(response => response.data);
}

//获取图片key
export const getImgKey = params => {
	return configImgKey;
//	return HTTP.get('/static/mockJson/config.json').then(response => response.data);
}






// 用户修改密码
export const modifyPassword = params => {
	// return HTTP.get('/tenants/'+params.checkPass+'/'+params.newPwd+'/'+params.account).then(response => response.data)
}	
// 验证密码
//export const deletePass = params => {
//	return HTTP.get('/tenants/isPwdTrue/'+params.passWord+'/'+params.loginName).then(response => response.data)
//}

	

